1 module dataframe.common;
2 import std.conv;
3 import std.csv;
4 import std.datetime;
5 import std.exception;
6 import std.range:array, stride,only;
7 import std.stdio;
8 import std.variant;
9 import std.string:isNumeric;
10 alias KalVariant=Algebraic!(string,int,long, DateTime, float,double);
11 import std.typecons:tuple,Tuple;
12 
13 debug=1;
14 
15 enum ColumnType
16 {
17 	Int,
18 	Long,
19 	Double,
20 	Date,
21 	DateTime,
22 	String
23 }
24 
25 alias KalType=string;
26 
27 bool isDouble(KalVariant[] data)
28 {
29 	import std.string:strip,isNumeric;
30 	foreach(cell;data)
31 	{
32 		auto s=cell.to!string;
33 		if ((!s.strip.length==0) && (!s.isNumeric))
34 			return false;
35 	}
36 	return true;
37 }
38 
39 bool isInteger(T=int)(KalVariant[] data)
40 if (is(T==int)||is(T==long))
41 {
42 	foreach(cell;data)
43 	{
44 		try
45 		{
46 			auto s=cell.get!string;
47 			auto z=(s).to!T;
48 		}
49 		catch(Exception e)
50 		{
51 			return false;
52 		}
53 	}
54 	return true;
55 }
56 
57 bool isDate(T=Date)(KalVariant[] data)
58 if(is(T==Date)||is(T==DateTime)||is(T==SysTime))
59 {
60 	foreach(cell;data)
61 	{
62 		try
63 		{
64 			auto s=cell.get!DateTime;
65 			continue;
66 		}
67 		catch(Exception e)
68 		{
69 
70 		}
71 		auto s=cell.get!string;
72 		if (s.isNumberDate)
73 			continue;
74 		try
75 		{
76 			auto z=SysTime.fromSimpleString(s);
77 			continue;
78 		}
79 		catch(Exception e)
80 		{
81 			return false;
82 		}
83 	}
84 	return true;
85 }
86 
87 
88 bool isNumberDate(string s)
89 {
90 	import std.string:strip;
91 	s=s.strip;
92 	if ((!s.isNumeric) || (s.length!=8))
93 		return false;
94 	auto y=s[0..4].to!int;
95 	auto m=s[4..6].to!int;
96 	auto d=s[6..8].to!int;
97 	if ((y>1900) && (m>=1)&&(m<=12)&&(d>=1)&&(d<=31))
98 		return true;
99 	return false;
100 }
101 
102 
103 
104 Date dateTimeToDate(DateTime st)
105 {
106 	return std.datetime.Date(st.year,st.month,st.day);	
107 }
108 Date stringToDate(string s)
109 {
110 	auto st=SysTime.fromSimpleString(s);
111 	return std.datetime.Date(st.year,st.month,st.day);
112 }
113 DateTime stringToDateTime(string s)
114 {
115 	auto st=SysTime.fromSimpleString(s);
116 	return std.datetime.DateTime(st.year,st.month,st.day,st.hour,st.minute,st.second);
117 }
118 
119 DateTime dateToDateTime(Date d)
120 {
121 	return std.datetime.DateTime(d.year,d.month,d.day,0,0,0);
122 }
123 
124 bool isDashYYYYMMDD(string s)
125 {
126 	import std.string:strip,isNumeric;
127 	s=s.strip;
128 	if (s.length!=10)
129 		return false;
130 	if ((s[4]!='-') || (s[7]!='-'))
131 		return false;
132 	if ((!s[0..4].isNumeric)||(!s[5..7].isNumeric)||(!s[8..10].isNumeric))
133 		return false;
134 	auto m=(s[5..7]).to!int;
135 	if((m<1)||(m>12))
136 		return false;
137 	auto d=s[8..10].to!int;
138 	if((d<1)||(d>31))
139 		return false;
140 	return true;
141 }
142 
143 Date parseDashYYYYMMDD(T:Date)(string s)
144 {
145 	import std.string:strip;
146 	s=s.strip;
147 	auto y=s[0..4].to!int;
148 	auto m=s[5..7].to!int;
149 	auto d=s[8..10].to!int;
150 	return std.datetime.Date(y,m,d);
151 }
152 
153 DateTime parseDashYYYYMMDD(T:DateTime)(string s)
154 {
155 	import std.string:strip;
156 	s=s.strip;
157 	auto y=s[0..4].to!int;
158 	auto m=s[5..7].to!int;
159 	auto d=s[8..10].to!int;
160 	return std.datetime.DateTime(y,m,d,0,0,0);
161 }
162 Date parseDate(T:Date)(string s)
163 {
164 	if (s.isNumberDate)
165 		return s.numberDate!Date;
166 	if (s.isDashYYYYMMDD)
167 		return s.parseDashYYYYMMDD!Date;
168 	auto t=SysTime.fromSimpleString(s);
169 	return std.datetime.Date(t.year,t.month,t.day);
170 }
171 
172 DateTime parseDate(T:DateTime)(string s)
173 {
174 	if (s.isNumberDate)
175 		return s.numberDate!DateTime;
176 	if (s.isDashYYYYMMDD)
177 		return s.parseDashYYYYMMDD!DateTime;
178 	auto t=SysTime.fromSimpleString(s);
179 	return std.datetime.DateTime(t.year,t.month,t.day,t.hour,t.minute,t.second);
180 }
181 DateTime numberDate(T:DateTime)(string s)
182 {
183 	import std.string:strip;
184 	s=s.strip;
185 	enforce((s.isNumeric) && (s.length==8));
186 	auto y=s[0..4].to!int;
187 	auto m=s[4..6].to!int;
188 	auto d=s[6..8].to!int;
189 	return std.datetime.DateTime(y,m,d,0,0,0);
190 }
191 Date numberDate(T:Date)(string s)
192 {
193 	import std.string:strip;
194 	s=s.strip;
195 	enforce((s.isNumeric) && (s.length==8));
196 	auto y=s[0..4].to!int;
197 	auto m=s[4..6].to!int;
198 	auto d=s[6..8].to!int;
199 	return std.datetime.Date(y,m,d);
200 }
201 
202 
203 string toString(KalVariant[] cells)
204 {
205 	string ret;
206 	foreach(cell;cells)
207 		ret~=cell.to!string~"\n";
208 	return ret;
209 }
210 void log(string s)
211 {
212 	writefln("%s",s);
213 	stdout.flush;
214 }
215 
216 private size_t peekCols(string data, char separator=',')
217 {
218 	import std.string:indexOf,split;
219 	auto i=indexOf(data,"\n");
220 	return (i==-1)?0:(data[0..i].split([separator]).length);
221 }
222 private string[] peekHeaderCols(string data, char separator=',')
223 {
224 	import std.string:indexOf,split;
225 	auto i=indexOf(data,"\n");
226 	return (i==-1)?[]:(data[0..i].split([separator]));
227 }